package com.zidian.leader.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;

import com.zidian.leader.R;
import com.zidian.leader.api.HttpFactory;
import com.zidian.leader.common.util.HawkUtils;
import com.zidian.leader.entity.LoginResult;

import java.lang.ref.WeakReference;

import rx.Subscriber;
import rx.Subscription;

/**
 * 欢迎界面
 * Created by GongCheng on 2016/11/2.
 */

public class LoadActivity extends AppCompatActivity {
    private Subscription subscription;
    private StaticHandler handler = new StaticHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_load);
    }

    @Override
    protected void onResume() {
        super.onResume();

        handler.sendEmptyMessageDelayed(1 , 2000);
    }

    /**
     * 静态内部类handler
     */
    static class StaticHandler extends Handler {
        WeakReference<LoadActivity> activityWeakReference;

        StaticHandler(LoadActivity activity) {
            activityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            LoadActivity activity = activityWeakReference.get();
            super.handleMessage(msg);
            if (activity != null) {
                switch (msg.what) {
                    case 1:
                        if (HawkUtils.getIsLogin()) {
                            activity.startActivity(new Intent(activity , MainActivity.class));
                            activity.overridePendingTransition(android.R.anim.fade_in , android.R.anim.fade_out);
                        } else {
                            activity.startActivity(new Intent(activity , LoginActivity.class));
                            activity.overridePendingTransition(android.R.anim.fade_in , android.R.anim.fade_out);
                        }
                        break;
                }
            }
        }
    }

    /**
     * 通过保存的用户名和密码登录
     */
    private void login(){
        String username = HawkUtils.getUsername();
        String password = HawkUtils.getPassword();
        subscription = HttpFactory.getInstance().login(username, password)
                .subscribe(new Subscriber<LoginResult>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                startActivity(new Intent(LoadActivity.this,LoginActivity.class));
                //登录失败，清除用户缓存
                HawkUtils.clearPreferences();
            }

            @Override
            public void onNext(LoginResult loginResult) {
                HawkUtils.setToken(loginResult.getToken());
                startActivity(new Intent(LoadActivity.this,MainActivity.class));
                HawkUtils.setColleges(loginResult.getData().getCollegeList());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (subscription != null && subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }
}
